<script>
import {formatTime} from "@/utils/formatTime.js";
import {all, del, add, edit, filter} from '@/api/recyclers.js'

export default {
  name: "recyclers",
  methods: {
    formatTime,
    refresh() {
      all().then(res => {
        if (res.code === 200) {
          this.recyclersData = res.data
        } else {
          this.$message.error("获取数据失败")
        }
      })
    },
    handleDelete(id) {
      del(id).then(res => {
        if (res.code === 200) {
          this.$message.success("删除成功")
          this.refresh()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleEdit(data) {
      this.editRecyclersFormData = data;
      this.editRecyclersDialogVisible = true;
    },
    editRecyclers(data) {
      edit(data).then(res => {
        if (res.code === 200) {
          this.$message.success("修改成功")
          this.refresh()
          this.editRecyclersDialogVisible = false
          this.editRecyclersFormData = {}
        } else {
          this.$message.error("修改失败")
        }
      })
    },
    addRecyclers(data) {
      add(data).then(res => {
        if (res.code === 200) {
          this.$message.success("添加成功")
          this.refresh()
          this.addRecyclersDialogVisible = false
          this.addRecyclersFormData = {}
        } else {
          this.$message.error("添加失败")
        }
      })
    },
    filterRecyclers(data) {
      filter(data).then(res => {
        if (res.code === 200) {
          this.recyclersData = res.data
          this.filterRecyclersDialogVisible = false
        } else {
          this.$message.error("获取数据失败")
        }
      })
    }
  },
  data() {
    return {
      recyclersData: [],
      filterRecyclersDialogVisible: false,
      editRecyclersDialogVisible: false,
      addRecyclersDialogVisible: false,
      filterRecyclersFormData: {},
      editRecyclersFormData: {},
      addRecyclersFormData: {},
    }
  },
  created() {
    this.refresh()
  }
}
</script>

<template>
  <div id="recyclers-title">
    二手回收商管理
  </div>
  <div id="recyclers-box">
    <div id="recyclers-form">
      <el-button type="primary" @click="filterRecyclersDialogVisible = true">增加检索</el-button>
      <el-button type="primary" @click="addRecyclersDialogVisible = true">新增数据</el-button>
      <el-button type="primary">导入数据</el-button>
      <el-button type="primary">导出数据</el-button>
      <el-button type="primary" @click="refresh">刷新</el-button>
    </div>
    <div>
      <el-table id="recyclers-data" :data="recyclersData">
        <el-table-column type="selection" width="50px"/>
        <el-table-column label="回收商名称" prop="name" width="150"></el-table-column>
        <el-table-column label="回收商电话" prop="phone" width="200"></el-table-column>
        <el-table-column label="回收商微信" prop="weixin" width="200"></el-table-column>
        <el-table-column label="负责校区" prop="campus" width="200"></el-table-column>
        <el-table-column label="回收物品" prop="goods" width="200"></el-table-column>
        <el-table-column label="创建时间" prop="createTime" width="200">
          <template #default="scope">
            <span>{{ formatTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="修改时间" prop="updateTime" width="200">
          <template #default="scope">
            <span>{{ formatTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template #default="scope">
            <el-button @click="handleEdit(scope.row)" type="primary">编辑</el-button>
            <el-button @click="handleDelete(scope.row.id)" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <!-- 修改回收商信息 -->
  <div>
    <el-dialog title="修改评论信息" v-model="editRecyclersDialogVisible" :close-on-click-modal="true">
      <el-form :model="editRecyclersFormData">
        <el-form-item label="回收商名称">
          <el-input v-model="editRecyclersFormData.name"></el-input>
        </el-form-item>
        <el-form-item label="回收商电话">
          <el-input v-model="editRecyclersFormData.phone"></el-input>
        </el-form-item>
        <el-form-item label="回收商微信">
          <el-input v-model="editRecyclersFormData.weixin"></el-input>
        </el-form-item>
        <el-form-item label="负责校区">
          <el-select v-model="editRecyclersFormData.campus" placeholder="请选择">
            <el-option label="奉贤校区" value="奉贤校区"></el-option>
            <el-option label="徐汇校区" value="徐汇校区"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="回收物品">
          <el-input v-model="editRecyclersFormData.goods"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="editRecyclersDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="editRecyclers(editRecyclersFormData)">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
  <!-- 添加回收商  -->
  <div>
    <el-dialog title="添加回收商" v-model="addRecyclersDialogVisible" :close-on-click-modal="true">
      <el-form :model="addRecyclersFormData">
        <el-form-item label="回收商名称">
          <el-input v-model="addRecyclersFormData.name"></el-input>
        </el-form-item>
        <el-form-item label="回收商电话">
          <el-input v-model="addRecyclersFormData.phone"></el-input>
        </el-form-item>
        <el-form-item label="回收商微信">
          <el-input v-model="addRecyclersFormData.weixin"></el-input>
        </el-form-item>
        <el-form-item label="负责校区">
          <el-select v-model="addRecyclersFormData.campus" placeholder="请选择">
            <el-option label="奉贤校区" value="奉贤校区"></el-option>
            <el-option label="徐汇校区" value="徐汇校区"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="回收物品">
          <el-input v-model="addRecyclersFormData.goods"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="addRecyclersDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addRecyclers(addRecyclersFormData)">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>

  <!-- 过滤 -->
  <div>
    <el-dialog title="条件筛选" v-model="filterRecyclersDialogVisible" :close-on-click-modal="true">
      <el-form :model="filterRecyclersFormData">
        <el-form-item label="回收商名称">
          <el-input v-model="filterRecyclersFormData.name"></el-input>
        </el-form-item>
        <el-form-item label="回收商电话">
          <el-input v-model="filterRecyclersFormData.phone"></el-input>
        </el-form-item>
        <el-form-item label="回收商微信">
          <el-input v-model="filterRecyclersFormData.weixin"></el-input>
        </el-form-item>
        <el-form-item label="负责校区">
          <el-select v-model="filterRecyclersFormData.campus" placeholder="请选择">
            <el-option label="奉贤校区" value="奉贤校区"></el-option>
            <el-option label="徐汇校区" value="徐汇校区"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="回收物品">
          <el-input v-model="filterRecyclersFormData.goods"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="filterRecyclersDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="filterRecyclers(filterRecyclersFormData)">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<style scoped>
#recyclers-box {
  background-color: #68a273;
  width: 90%;
  height: 90%;
  margin-top: 2%;
  margin-left: 3%;
  border-radius: 30px;
}

#recyclers-title {
  font-size: 30px;
  margin-left: 20px;
}

#recyclers-form {
  display: flex;
  padding-top: 40px;
  justify-content: flex-end;
  margin-right: 80px;
  margin-bottom: 10px;
}
</style>